<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料子编号" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入物料子编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="spec">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <!-- <el-form-item label="供应商" prop="supplyId">
        <el-select style="width: 100%" v-model="queryParams.supplyId" placeholder="供应商" >
          <el-option
            v-for="dict in supplyOptions"
            :key="dict.id"
            :label="dict.supplyName"
            :value="dict.id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="stockId">
        <el-select clearable  v-model="queryParams.stockId" placeholder="请选择仓库" clearable>
          <el-option
            v-for="dict in warehouseinfo"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="stockinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料名称" width="140" align="center" prop="jcItemName" />
      <el-table-column label="物料编号" show-overflow-tooltip width="140" align="center" prop="itemCode" />
      <el-table-column label="物料子编号" show-overflow-tooltip width="140" align="center" prop="itemId" />
      <el-table-column label="规格型号" show-overflow-tooltip width="120" align="center" prop="spec" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="批次号" align="center" prop="lot" />
      <el-table-column label="库存数量" align="center" prop="kcNum" />
      <el-table-column label="锁账数量" align="center" prop="lockNum" />
      <el-table-column label="可用数量" align="center" prop="kyNum" >
        <template slot-scope="scope">
          <div>{{scope.row.kcNum - scope.row.lockNum}}</div>
        </template>
      </el-table-column>
     <!-- <el-table-column label="供应商" align="center" prop="supplyName" />-->
      <el-table-column label="大类" align="center" prop="className" />
      <el-table-column label="仓库" width="150" show-overflow-tooltip  align="center" prop="stockName" />
      <el-table-column label="库区" width="150" show-overflow-tooltip  align="center" prop="kuquName" />
      <el-table-column label="库位" align="center" prop="kuweiCode" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="物料名称" prop="itemName">
          <el-input v-model="form.itemName" readonly="true" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料号" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入物料号" />
        </el-form-item>
        <el-form-item label="库存数量" prop="kcNum">
          <el-input v-model="form.kcNum" placeholder="请输入库存数量" />
        </el-form-item>

        <el-form-item label="供应商" prop="supplyId">
          <el-input v-model="form.supplyId" readonly="true" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="物料类型" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入大类编码" />
        </el-form-item>
        <el-form-item label="仓库" prop="stockId">
          <el-input v-model="form.stockId" readonly="true" placeholder="请选择仓库" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectListStockinfo, getStockinfo, delStockinfo, addStockinfo, updateStockinfo } from "@/api/business/stockinfo";
import { listClass} from "@/api/base/class";
import { listSupplier} from "@/api/supplychain/supplier";
import { getListWarehouseinfo } from "@/api/base/warehouseinfo";
export default {
  name: "StockinfoSelect",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存信息表格数据
      stockinfoList: [],
      classOptions: [],
      supplyOptions:[],
      warehouseinfo:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        itemName: null,
        itemId: null,
        itemCode: null,
        lot: null,
        kcNum: null,
        qutlityStauts: null,
        supplyId: null,
        supplyName: null,
        classId: null,
        className: null,
        stockId: null,
        stockName: null,
        version: null,
        codesParams:[],
        lotsParams:[],
        spec:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    //this.getList();
    listClass().then(response => {
      this.classOptions = response.rows;
    });
    listSupplier().then(response => {
      this.supplyOptions = response.rows;
    });
    getListWarehouseinfo().then(response => {
      this.warehouseinfo = response.data;
    });
  },
  methods: {
    /** 查询库存信息列表 */
    getList() {
      this.loading = true;
      selectListStockinfo(this.queryParams).then(response => {
        this.stockinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getStockinfoList(codesParams,lotsParams,classId){
      this.queryParams.codesParams=codesParams;
      this.queryParams.lotsParams=lotsParams;
      this.queryParams.classId=classId;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        itemName: null,
        itemId: null,
        lot: null,
        kcNum: null,
        qutlityStauts: null,
        supplyId: null,
        supplyName: null,
        classId: null,
        className: null,
        stockId: null,
        stockName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStockinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStockinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStockinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库存信息编号为"' + ids + '"的数据项？').then(function() {
        return delStockinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/stockinfo/export', {
        ...this.queryParams
      }, `stockinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
